//
//  HAODE
//
//  Created by panquanReabam.com on 15/9/23.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "WhsInAddViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "UIImageView+WebCache.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "GoodsView.h"
#import "WhsInListViewController.h"
#import "WhsOutListViewController.h"
#import "WhsWhsItemListViewController.h"
#import "OptionNameListViewController.h"
#import "CheckVouchListViewController.h"
#import "WhsInAddView.h"
#import "WhsInAddModel.h"
#import "PlatformProductTypeListViewController.h"
#import "HasSpecFmdbTool.h"
#import "NoSpecFmdbTool.h"
#import "NeedOrderListViewController.h"
#import "AllotOrderListViewController.h"
#import "MemberListViewController.h"
#import "huiyuanDetailsCell.h"
#import "OrderDetailM.h"
#import "RefundListViewController.h"
#import "TheRadioGuideListViewController.h"
@interface WhsInAddViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UITextViewDelegate,UISearchBarDelegate,UIScrollViewDelegate,WhsInAddViewDelegate,OptionNameListViewControllerDelegate,WhsWhsItemListViewControllerDelegate,PlatformProductTypeListViewControllerDelegate,MemberListViewControllerDelegate,UITableViewDelegate,UITableViewDataSource,TheRadioGuideListViewControllerDelegate>
@property (strong, nonatomic) NSString*Code;

@property (strong, nonatomic) NSString*staffId;
@property (strong, nonatomic) NSString*RefundTypeName;
@property (strong, nonatomic) NSString*itemType;
@property (strong, nonatomic) NSString*whsId;

@property (strong, nonatomic) NSString*outWhsId;
@property (strong, nonatomic) NSString*inWhsId;
@end

@implementation WhsInAddViewController
{
    WhsInAddView*AddView;
    UIScrollView*MYmainScroll;
    NSString *userToken;
    NSString*allotType;
    UITableView*table;
    UILabel * remarkLabel;
    UIImageView*imagess;
    UIView* myToolBar;
    NSString*memberId;
    UIButton*totalQuantity;
    UIButton*diseView;
    UIButton* quantityBtn;
    NSMutableArray *refundDealPriceArray;
    NSMutableArray *PriceArray;
    NSString*staffName;
    NSString*staffId;
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    userToken = [user objectForKey:@"tokenId"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
   
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
}
-(void)checkVouchType:(NSString *)checkVouchType checkVouchTypeCode:(NSString *)checkVouchTypeCode{
    self.Code=checkVouchTypeCode;
    self.itemType=checkVouchType;
    [AddView.typeBtn setTitle:checkVouchType forState:UIControlStateNormal];
    
}
- (void)NeedOrderType:(NSString *)NeedOrderType NeedOrderCode:(NSString *)NeedOrderCode{
    self.Code=NeedOrderCode;
    [AddView.typeBtn setTitle:NeedOrderType forState:UIControlStateNormal];
}
-(void)outWhsName:(NSString *)outWhsName outWhsId:(NSString *)outWhsId{
    self.outWhsId=outWhsId;
    [AddView.whsBtn setTitle:outWhsName forState:UIControlStateNormal];
}

-(void)inWhsName:(NSString *)inWhsName inWhsId:(NSString *)inWhsId{
    self.inWhsId=inWhsId;
    [AddView.whsBtn1 setTitle:inWhsName forState:UIControlStateNormal];
}
-(void)Whs:(NSString *)Whs WhsCode:(NSString *)WhsCode{
    
    self.whsId=WhsCode;
    [AddView.whsBtn setTitle:Whs forState:UIControlStateNormal];
    
}

-(void)staffId:(NSString *)staffIdStr staffName:(NSString *)staffNameStr{
    self.staffId=staffIdStr;
    [AddView.staffNameBtn setTitle:staffNameStr forState:UIControlStateNormal];
    
}
-(void)RefundType:(NSString *)RefundType RefundTypeCode:(NSString *)RefundTypeCode{
    
    self.Code=RefundTypeCode;
    self.RefundTypeName=RefundType;
    [AddView.typeBtn setTitle:RefundType forState:UIControlStateNormal];
    
}
-(void)WhsInType:(NSString *)WhsInType WhsInTypeCode:(NSString *)WhsInTypeCode{
    self.Code=WhsInTypeCode;
    [AddView.typeBtn setTitle:WhsInType forState:UIControlStateNormal];
    if ([WhsInType isEqualToString:@"总部调拨"]) {
        allotType=@"G";
    }else{
        allotType=@"C";

    }
}
- (void)viewDidLoad
{
    
    [super viewDidLoad];
    if ([self.saveTypeStr isEqualToString:@"NeedOrder"]) {
        self.Type=@"NeedOrderType";
        self.title = @"确认要货";
        
    }else if ([self.saveTypeStr isEqualToString:@"CheckVouch"]){
        self.Type=@"CheckVoucType";
        self.title = @"确认录入";
       
    }else if ([self.saveTypeStr isEqualToString:@"WhsOut"]){
        
        self.Type=@"WhsOutType";
        self.title = @"确认出库";
        
    }else if ([self.saveTypeStr isEqualToString:@"WhsIn"]){
        self.Type=@"WhsInType";
        self.title = @"确认入库";
     
    }else if ([self.saveTypeStr isEqualToString:@"Allot"]){
        self.Type=@"AllotOrderType";
        self.title = @"确认调拨";
      
    }else if ([self.saveTypeStr isEqualToString:@"Refund"]){
        self.Type=@"RGType";
        self.title = @"确认退货";
      self.RefundTypeName=@"";
    }
    refundDealPriceArray=[[NSMutableArray alloc]init];
    PriceArray=[[NSMutableArray alloc]init];
    PriceArray=[self.dealPriceArray copy];
    [self AddView1];
}
-(void)AddView1{
   
    AddView=[[WhsInAddView alloc]init];
    AddView.mDelegate=self;
    AddView.specNameArray=self.specNameArray;
    AddView.itemCodeArray=self.itemCodeArray;
    AddView.itemNameArray=self.itemNameArray;
    AddView.specIdArray=self.specIdArray;
    AddView.dealPriceArray=self.dealPriceArray;
    AddView.quantityArray=self.quantityArray;
    AddView.headImageFullArray=self.headImageFullArray;
    refundDealPriceArray=self.dealPriceArray;
    WhsInAddModel *dataModel=[WhsInAddModel dataModelWithWhsInAddtDict:self.saveTypeStr];
    AddView.model=dataModel;
    AddView.frame=CGRectMake(0, 0, ScreenWidth, AddView.heightView.bottom);
   
  
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.backgroundColor=zuiqianhuiColor;
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    table.tableHeaderView=AddView;
    
        UIView*tableFooter=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
        tableFooter.backgroundColor=[UIColor whiteColor];
    
        table.tableFooterView=tableFooter;
        UIView*gayView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        gayView.backgroundColor=zuiqianhuiColor;
        [tableFooter addSubview:gayView];
        UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth, 0.40)];
        lineLabel.backgroundColor=qianhuiColor;
        [gayView addSubview:lineLabel];
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 100-0.40, ScreenWidth, 0.45)];
        lineLabel1.backgroundColor=qianhuiColor;
        [gayView addSubview:lineLabel1];

    
        _remark=[[UITextView alloc]init];
        _remark.frame=CGRectMake(20, 20, ScreenWidth-40, 80);
        _remark.scrollEnabled = YES;//是否可以拖动
        _remark.delegate=self;
        _remark.font = [UIFont fontWithName:@"Arial" size:15.0];//设置字体名字和字体大小
        _remark.layer.borderWidth =0.0;
        CGColorSpaceRef registedSpace11 = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref111 = CGColorCreate(registedSpace11,(CGFloat[]){ 0, 0, 0, 0.3 });
        //tv.keyboardType = UIKeyboardTypePhonePad;
        [_remark.layer setBorderColor:tureBTncolorref111];//边框颜色
        _remark.layer.cornerRadius =0;
        [tableFooter addSubview:_remark];
        imagess=[[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 20, 16)];
        imagess.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
        [_remark addSubview:imagess];
    
    
    
        remarkLabel=[[UILabel alloc]init];
        remarkLabel.frame =CGRectMake(23, 7, ScreenWidth-60, 14);
        remarkLabel.text = @"备注";
        remarkLabel.textColor=qianhuiColor;
        remarkLabel.font=[UIFont systemFontOfSize:14];
        [_remark addSubview:remarkLabel];
    
    myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:keyboardFot];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];
    _remark.inputAccessoryView = myToolBar;
   
    [self addbuyBtn];
    if ([self.saveTypeStr isEqualToString:@"CheckVouch"]) {//盘点
        [self itemTypes];
    }

}

#pragma mark - Table view data source
#pragma mark - Table view delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.quantityArray count];
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.saveTypeStr isEqualToString:@"Refund"]){
   
           return  275/2;
    }


        return  240/2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    huiyuanDetailsCell* cell = (huiyuanDetailsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"huiyuanDetailsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    
    if (cell==nil) {
        
    }else{
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIView*goodsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 120)];
    UIImageView*headImageFull = [[UIImageView alloc]init];
    [headImageFull.layer setCornerRadius:4.0];
    [headImageFull.layer setBorderWidth:0.5];
    [headImageFull.layer setMasksToBounds:YES];
    [headImageFull setImageWithURL:[NSURL URLWithString:  self.headImageFullArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"zhengfangxing"]];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0.93, 0.93, 0.93, 0.93 });
    [headImageFull.layer setBorderColor:colorref2];//边框颜色
    headImageFull.frame = CGRectMake(15, 17.5, 82, 82);
    [goodsView addSubview:headImageFull];
    
    
    NSString *s2 =[NSString stringWithFormat:@"%@", self.itemNameArray[indexPath.row]];
    UIFont *font1 = [UIFont systemFontOfSize:13];
    CGSize size1 = CGSizeMake(ScreenWidth-82-15-30, 2000);
    CGSize labelsize1 = [s2 sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];
    UILabel*itemName = [[UILabel alloc]init];
    itemName.numberOfLines=0;
    itemName.text=s2;
    itemName.frame = CGRectMake( headImageFull.right+10, 17.5, ScreenWidth-82-15-30, labelsize1.height);
    itemName.textColor=qianheiColor;
    itemName.font=[UIFont systemFontOfSize:13];
    [goodsView addSubview:itemName];
    
    
    UILabel*itemType = [[UILabel alloc]init];
    itemType.frame = CGRectMake( headImageFull.right+10, 68, ScreenWidth-120, 13);
    itemType.font=[UIFont systemFontOfSize:12];
    itemType.textColor=shenhuiColor;
    itemType.text=[NSString stringWithFormat:@"%@",self.specNameArray[indexPath.row]];
    [goodsView addSubview:itemType];
    
    
    
    NSString *quantitStr =[NSString stringWithFormat:@"数量 %@",self.quantityArray[indexPath.row]];
    UIFont *quantitStrfont1 = [UIFont systemFontOfSize:13];
    CGSize quantitStrsize1 = CGSizeMake(ScreenWidth-82-15-30, 2000);
    CGSize quantitStrlabelsize1 = [quantitStr sizeWithFont:quantitStrfont1 constrainedToSize:quantitStrsize1 lineBreakMode:NSLineBreakByCharWrapping];
    
    UILabel*quantity = [[UILabel alloc]init];
    quantity.frame = CGRectMake( headImageFull.right+10, 85, quantitStrlabelsize1.width,12);
    quantity.font=[UIFont systemFontOfSize:12];
    quantity.textColor=shenhuiColor;
    quantity.text=[NSString stringWithFormat:@"数量 %@",self.quantityArray[indexPath.row]];
    [goodsView addSubview:quantity];
    
    
    UILabel*dealPrice = [[UILabel alloc]init];
    dealPrice.frame = CGRectMake(quantity.right+20, 85, 230,13);
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString*prc=self.dealPriceArray[indexPath.row];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:prc.doubleValue]];
    dealPrice.text=[NSString stringWithFormat:@"¥ %@",formattedNumberString];
    
    dealPrice.textColor=qianheiColor;
    dealPrice.font=[UIFont systemFontOfSize:13];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@" ,formattedNumberString]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:12]
     
                          range:NSMakeRange(0, 1)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:shenhuiColor
     
                          range:NSMakeRange(0, 1)];
    dealPrice.attributedText = AttributedStr;
    
    [goodsView addSubview:dealPrice];
    
    if ([self.saveTypeStr isEqualToString:@"Refund"]){
        
        if (self.dealPriceArray.count !=indexPath.row+1) {
            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10,275/2-0.40, ScreenWidth-20, 0.40)];
            lineLabel1.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel1];
            
        }
        
        if (self.dealPriceArray.count ==indexPath.row+1) {
            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0,275/2-0.40, ScreenWidth, 0.40)];
            lineLabel1.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel1];
  
        }

        
        UILabel*redfund= [[UILabel alloc]init];
        redfund.frame = CGRectMake( ScreenWidth/2+20, 110, 50, 13);
        redfund.font=[UIFont systemFontOfSize:12];
        redfund.textColor=shenhuiColor;
        redfund.text=@"退货单价";
        [goodsView addSubview:redfund];
        
        cell.refundText.frame=CGRectMake(redfund.right+5, 100, ScreenWidth/2-60-30,30);
        cell.refundText.text=[NSString stringWithFormat:@" %@", refundDealPriceArray[indexPath.row]];
        cell.refundText.textColor=hongseColor;
        cell.refundText.font=[UIFont systemFontOfSize:15];
        cell.refundText.tag=indexPath.row;
        cell.refundText.delegate=self;
        cell.refundText.keyboardType=UIKeyboardTypeDecimalPad;
        [cell.refundText.layer setCornerRadius:2];
        [cell.refundText.layer setBorderWidth:0.5];
        [cell.refundText.layer setMasksToBounds:YES];
        cell.refundText.layer.borderColor=[[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1] CGColor];
        cell.refundText.inputAccessoryView = myToolBar;
        [goodsView addSubview:cell.refundText];

        
    }else{
    
        if (self.dealPriceArray.count !=indexPath.row+1) {
            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10,240/2-0.45, ScreenWidth-20, 0.45)];
            lineLabel1.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel1];
            
        }
        
        if (self.dealPriceArray.count ==indexPath.row+1) {
            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0,240/2-0.45, ScreenWidth, 0.45)];
            lineLabel1.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel1];
        }

    }
    

   
    
    [cell.contentView addSubview:goodsView];
    

    return cell;
}

-(void)chooesBtn {

    MemberListViewController *detailViewController = [[MemberListViewController alloc] init];
    detailViewController.delegate = self;
    detailViewController.chooseState=@"选择";
    [self.navigationController pushViewController:detailViewController animated:YES];

}
-(void)memberIdStr:(NSString *)memberIdStr userName:(NSString *)userName phone:(NSString *)phone sex:(NSString *)sex  gradeName:(NSString *)gradeName consigneeName:(NSString *)consigneeName consigneeAddr:(NSString *)consigneeAddr consigneePhone:(NSString *)consigneePhone mdiscountMoney:(NSString *)mdiscountMoney{
        memberId=memberIdStr;
        AddView.userName=userName;
        AddView.phone=phone;
        AddView.sex=sex;
        AddView.gradeName=gradeName;
        AddView.RefundTypeName=self.RefundTypeName;
        WhsInAddModel *dataModel=[WhsInAddModel dataModelWithWhsInAddtDict:self.saveTypeStr];
        AddView.model=dataModel;

}

-(void)Refund:(UIButton *)sender {

      
    if ([AddView.typeBtn.titleLabel.text length] < 1) {
        [self showAlertMessage:@"请选择类型！"];
    }
    else{
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:userToken forKey:@"tokenId"];
        NSMutableArray *mDocumentLine=[NSMutableArray array];
        for (int i=0; i<self.specIdArray.count; i++) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSString *itemId = [self.itemIdArray objectAtIndex:i];
            NSString *quantity =[self.quantityArray objectAtIndex:i];
            NSString *specId =[NSString stringWithFormat:@"%@",[self.specIdArray objectAtIndex:i]];
            NSString *salePrice = [PriceArray objectAtIndex:i];
            NSString *refundMoney =[refundDealPriceArray objectAtIndex:i];
            
            [dic setValue:quantity forKey:@"quantity"];
            [dic setValue:itemId forKey:@"itemId"];
            [dic setValue:salePrice forKey:@"salePrice"];
            [dic setValue:refundMoney forKey:@"refundMoney"];
            if ([[NSString stringWithFormat:@"%@",specId ] isEqualToString:@""]) {
                specId=@"";
            }else{
                if ([[NSString stringWithFormat:@"%@",specId ] isEqualToString:@"<null>"]) {
                    specId=@"";
                }
                [dic setValue:specId forKey:@"specId"];
            }
            
            [mDocumentLine addObject:dic];
        }
            [params setValue:self.staffId forKey:@"staffId"];
            [params setValue:memberId forKey:@"memberId"];
            [params setValue:self.Code forKey:@"rgtype"];
            [params setValue:mDocumentLine  forKey:@"items"];
            [params setValue:_remark.text  forKey:@"remark"];
        NSLog(@"params-------%@",params);
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        [HttpRequest requestData:[NSString stringWithFormat:@"Business/%@/Add",@"newRefund"] params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    NSString *sqlStr1=[NSString stringWithFormat:@"DELETE FROM t_NoSpec WHERE userToken = '%@' AND typeString  = '%@'",userToken,_saveTypeStr];
                    NSString *sqlStr2=[NSString stringWithFormat:@"DELETE FROM t_Spec WHERE userToken = '%@' AND typeString  = '%@'",userToken,_saveTypeStr];
                    [NoSpecFmdbTool deleteData:sqlStr1];
                    [HasSpecFmdbTool deleteData:sqlStr2];
                    
                        RefundListViewController *detailViewController = [[RefundListViewController alloc] init];
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    
                    
                }else{
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
//                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    
                     [self showAlertMessage:requestResultStr];
                    
                }}
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
            
        }];
        
    }
}

//盘点 入库 出库 要货


-(void)ture:(UIButton *)sender {
    if ([AddView.typeBtn.titleLabel.text length] < 1) {
        [self showAlertMessage:@"请选择类型！"];
    } else{
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:userToken forKey:@"tokenId"];
        NSMutableArray *mDocumentLine=[NSMutableArray array];
        for (int i=0; i<self.specIdArray.count; i++) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSString *itemId = [self.itemIdArray objectAtIndex:i];
            NSString *quantity =[self.quantityArray objectAtIndex:i];
            NSString *specId =[NSString stringWithFormat:@"%@",[self.specIdArray objectAtIndex:i]];
            [dic setValue:quantity forKey:@"quantity"];
            [dic setValue:itemId forKey:@"itemId"];
            if ([[NSString stringWithFormat:@"%@",specId ] isEqualToString:@""]) {
                specId=@"";
            }else{
                if ([[NSString stringWithFormat:@"%@",specId ] isEqualToString:@"<null>"]) {
                    specId=@"";
                }
                [dic setValue:specId forKey:@"specId"];
            }
            
            [mDocumentLine addObject:dic];
            }
        
        if ([self.saveTypeStr isEqualToString:@"WhsOut"]) {//出库
            [params setValue:self.Code forKey:@"whsOutTypeCode"];
            [params setValue:self.whsId forKey:@"whsId"];
            [params setValue:mDocumentLine  forKey:@"itemList"];
            
        }
        if ([self.saveTypeStr isEqualToString:@"WhsIn"]) { //入库
            [params setValue:self.Code forKey:@"whsInTypeCode"];
            [params setValue:self.whsId forKey:@"whsId"];
            [params setValue:mDocumentLine  forKey:@"itemList"];
            
        }
        if ([self.saveTypeStr isEqualToString:@"CheckVouch"]) {//盘点
            
            [params setValue:@"" forKey:@"ckvId"];
            [params setValue:self.Code forKey:@"itemTypeCode"];
            [params setValue:AddView.typeBtn.titleLabel.text forKey:@"itemTypeName"];
            [params setValue:self.whsId forKey:@"whsId"];
            [params setValue:AddView.whsBtn.titleLabel.text forKey:@"whsName"];
            [params setValue:mDocumentLine  forKey:@"items"];
        }
        if ([self.saveTypeStr isEqualToString:@"NeedOrder"]) {//要货
            [params setValue:self.Code forKey:@"ntypeCode"];
            [params setValue:AddView.typeBtn.titleLabel.text forKey:@"ntypeName"];
            [params setValue:mDocumentLine  forKey:@"items"];
            
        }
        [params setValue:_remark.text  forKey:@"remark"];
        NSLog(@"params-------%@",params);
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        [HttpRequest requestData:[NSString stringWithFormat:@"Business/%@/Add",self.saveTypeStr] params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    NSString *sqlStr1=[NSString stringWithFormat:@"DELETE FROM t_NoSpec WHERE userToken = '%@' AND typeString  = '%@'",userToken,_saveTypeStr];
                    NSString *sqlStr2=[NSString stringWithFormat:@"DELETE FROM t_Spec WHERE userToken = '%@' AND typeString  = '%@'",userToken,_saveTypeStr];
                    [NoSpecFmdbTool deleteData:sqlStr1];
                    [HasSpecFmdbTool deleteData:sqlStr2];
                    
                    if ([self.saveTypeStr isEqualToString:@"CheckVouch"]) {
                        CheckVouchListViewController *detailViewController = [[CheckVouchListViewController alloc] init];
                        [self.navigationController pushViewController:detailViewController animated:YES];}
                    
                    if ([self.saveTypeStr isEqualToString:@"WhsOut"]) {
                        WhsOutListViewController *detailViewController = [[WhsOutListViewController alloc] init];
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }
                    if ([self.saveTypeStr isEqualToString:@"WhsIn"]) {
                        WhsInListViewController *detailViewController = [[WhsInListViewController alloc] init];
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }
                    if ([self.saveTypeStr isEqualToString:@"NeedOrder"]) {
                        NeedOrderListViewController *detailViewController = [[NeedOrderListViewController alloc] init];
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }
                    
                }else{
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                }}
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
            
        }];
        
    }
}
//调拨
-(void)AllotTure:(UIButton *)sender {
    if ([AddView.typeBtn.titleLabel.text length] < 1) {
        [self showAlertMessage:@"请选择类型！"];
    } else{
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:userToken forKey:@"tokenId"];
        NSMutableArray *mDocumentLine=[NSMutableArray array];
        for (int i=0; i<self.specIdArray.count; i++) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSString *itemId = [self.itemIdArray objectAtIndex:i];
            NSString *quantity =[self.quantityArray objectAtIndex:i];
            NSString *dealPrice =[self.dealPriceArray objectAtIndex:i];
            NSString *specId =[NSString stringWithFormat:@"%@",[self.specIdArray objectAtIndex:i]];
            [dic setValue:quantity forKey:@"quantity"];
            [dic setValue:dealPrice forKey:@"dealPrice"];
            [dic setValue:itemId forKey:@"itemId"];
            if ([[NSString stringWithFormat:@"%@",specId ] isEqualToString:@""]) {
                specId=@"";
            }else{
                if ([[NSString stringWithFormat:@"%@",specId ] isEqualToString:@"<null>"]) {
                    specId=@"";
                }
                [dic setValue:specId forKey:@"specId"];
            }
            
            [mDocumentLine addObject:dic];
        }
        

            [params setValue:self.Code forKey:@"typeCode"];
            [params setValue:AddView.typeBtn.titleLabel.text forKey:@"typeName"];
            [params setValue:self.outWhsId forKey:@"outWhsId"];
            [params setValue:AddView.whsBtn.titleLabel.text forKey:@"outWhsName"];
            [params setValue:self.inWhsId forKey:@"inWhsId"];
            [params setValue:AddView.whsBtn1.titleLabel.text forKey:@"inWhsName"];
            [params setValue:mDocumentLine  forKey:@"items"];
        
            [params setValue:_remark.text  forKey:@"remark"];
            NSLog(@"params-------%@",params);
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        [HttpRequest requestData:@"Allot/Order/Add" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    NSString *sqlStr1=[NSString stringWithFormat:@"DELETE FROM t_NoSpec WHERE userToken = '%@' AND typeString  = '%@'",userToken,_saveTypeStr];
                    NSString *sqlStr2=[NSString stringWithFormat:@"DELETE FROM t_Spec WHERE userToken = '%@' AND typeString  = '%@'",userToken,_saveTypeStr];
                    [NoSpecFmdbTool deleteData:sqlStr1];
                    [HasSpecFmdbTool deleteData:sqlStr2];
                    
                    AllotOrderListViewController *detailViewController = [[AllotOrderListViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }else{
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [self showAlertMessage:requestResultStr];
                    [Global hideProgressHUDInternal:self.view];
                }}
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
            
        }];
        
    }
}
-(void)AllotWhs1:(NSString *)whs{
    
    if (AddView.typeBtn.titleLabel.text.length>0) {
        WhsWhsItemListViewController *detailViewController = [[WhsWhsItemListViewController alloc] init];
        detailViewController.mDelegate=self;
        detailViewController.optionName=@"AllotoutWhs";
        detailViewController.allotType=allotType;
        [self.navigationController pushViewController:detailViewController animated:YES];

    }else{
    
        [self showAlertMessage:@"请先选择类型！"];
    }
   }
-(void)AllotWhs:(NSString *)whs{
    
    if (AddView.typeBtn.titleLabel.text.length>0) {
        WhsWhsItemListViewController *detailViewController = [[WhsWhsItemListViewController alloc] init];
        detailViewController.mDelegate=self;
        detailViewController.optionName=@"AllotinWhs";
        detailViewController.allotType=allotType;
        [self.navigationController pushViewController:detailViewController animated:YES];
    }else{
        [self showAlertMessage:@"请先选择类型！"];
    }

   }
-(void)whs:(NSString *)whs{
    WhsWhsItemListViewController *detailViewController = [[WhsWhsItemListViewController alloc] init];
    detailViewController.mDelegate=self;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

-(void)type:(NSString *)type{
    
    
    if ([self.saveTypeStr isEqualToString:@"CheckVouch"]) {
        PlatformProductTypeListViewController *detailViewController = [[PlatformProductTypeListViewController alloc] init];
        detailViewController.content=self.itemType;
        detailViewController.mDelegate=self;
        [self.navigationController pushViewController:detailViewController animated:YES];
    }else{
            
            OptionNameListViewController *detailViewController = [[OptionNameListViewController alloc] init];
            detailViewController.optionName=self.Type;
            detailViewController.titlename=@"选择类型";
            detailViewController.mDelegate=self;
            [self.navigationController pushViewController:detailViewController animated:YES];
            
            
        }
}
-(void)chooseUser:(NSString *)User{
    if ([self.saveTypeStr isEqualToString:@"Refund"]){
    TheRadioGuideListViewController *detailViewController = [[TheRadioGuideListViewController alloc] init];
    detailViewController.mDelegate = self;
    detailViewController.staffName=staffName;
    [self.navigationController pushViewController:detailViewController animated:YES];
    }
}

- (void)BeginEditing:(NSString *)BeginEditing{
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    MYmainScroll .frame =CGRectMake(0, -100, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];
    
}

-(void)OnTapDone:(NSString *)OnTapDone {
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    MYmainScroll .frame =CGRectMake(0,0, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];
    
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:3.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)addbuyBtn{
    [totalQuantity removeFromSuperview];
    [diseView removeFromSuperview];
    [quantityBtn removeFromSuperview];
    NSMutableArray*  sum=[[NSMutableArray alloc]init];
    for (int i=0; i<refundDealPriceArray.count; i++) {
        NSString *typeCode1 = [refundDealPriceArray objectAtIndex:i];
        NSString *isSingle1 = [self.quantityArray objectAtIndex:i];
        [sum addObject:[NSString stringWithFormat:@"%.2f",  [typeCode1 doubleValue]*[isSingle1 doubleValue]]];
    }
    
    NSNumber *sum11 = [sum valueForKeyPath:@"@sum.floatValue"];
    NSString*GoldPrice=[NSString stringWithFormat:@"%@",sum11];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
    totalQuantity=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-48, ScreenWidth*430/750, 45)];
    [totalQuantity setTitle:[NSString stringWithFormat:@"   合计 ¥ %@",formattedNumberString] forState:UIControlStateNormal];
    totalQuantity.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8];
    [totalQuantity setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    totalQuantity.contentHorizontalAlignment=UIControlContentHorizontalAlignmentFill;
    totalQuantity.contentVerticalAlignment=UIControlContentVerticalAlignmentBottom;
    totalQuantity.titleLabel.font=[UIFont systemFontOfSize:18];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"   合计 ¥ %@",formattedNumberString]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:13]
     
                          range:NSMakeRange(0, 7)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor whiteColor]
     
                          range:NSMakeRange(0, 7)];
    
    
    totalQuantity.titleLabel.attributedText = AttributedStr;
    
    quantityBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 9, ScreenWidth*430/750, 11)];
    NSNumber *sum1 = [self.quantityArray valueForKeyPath:@"@sum.floatValue"];
    [quantityBtn setTitle:[NSString stringWithFormat:@"    共 %@ 件商品",sum1] forState:UIControlStateNormal];
    [quantityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    quantityBtn.titleLabel.font=[UIFont systemFontOfSize:11];
    quantityBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [totalQuantity addSubview:quantityBtn];
    [self.view addSubview:totalQuantity];
   
    diseView=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-3, ScreenWidth*430/750, 3)];
    diseView.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8];
    [self.view addSubview:diseView];
    
    if ([self.saveTypeStr isEqualToString:@"Allot"]){
        UIButton*buyBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*430/750, ScreenHeight-48, ScreenWidth*320/750, 48)];
        [buyBtn setTitle:@"确定" forState:UIControlStateNormal];
        buyBtn.backgroundColor=luseColor;
        [buyBtn addTarget:self action:@selector(AllotTure:) forControlEvents:UIControlEventTouchUpInside];
        [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:buyBtn];

    }else  if ([self.saveTypeStr isEqualToString:@"Refund"]){

    

    
    UIButton*buyBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*430/750, ScreenHeight-48, ScreenWidth*320/750, 48)];
    [buyBtn setTitle:@"确定" forState:UIControlStateNormal];
    buyBtn.backgroundColor=luseColor;
    [buyBtn addTarget:self action:@selector(Refund:) forControlEvents:UIControlEventTouchUpInside];
    [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:buyBtn];
    }else {
 
        UIButton*buyBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*430/750, ScreenHeight-48, ScreenWidth*320/750, 48)];
        [buyBtn setTitle:@"确定" forState:UIControlStateNormal];
        buyBtn.backgroundColor=luseColor;
        [buyBtn addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
        [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:buyBtn];
    }
}
-(void)itemTypes{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    
    [param setValue:self.itemIdArray[0] forKey:@"itemId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Product/GetProductDetail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                self.Code=[severDataDict.dataDict objectForKey:@"itemTypeCode"];
                [AddView.typeBtn setTitle:[severDataDict.dataDict objectForKey:@"itemType"] forState:UIControlStateNormal];
                self.itemType=[severDataDict.dataDict objectForKey:@"itemType"];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
       [refundDealPriceArray replaceObjectAtIndex:textField.tag withObject:textField.text];
        [self addbuyBtn];

}
-(void)textFieldDidBeginEditing:(UITextField *)textField{

    
 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    table .frame =CGRectMake(0, -((textField.tag*125)+80), ScreenWidth, ScreenHeight);
    [UIView commitAnimations];

}
-(void)textViewDidBeginEditing:(UITextView *)textView{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        table .frame =CGRectMake(0, -300, ScreenWidth, ScreenHeight);
        [UIView commitAnimations];
    
    
}

//回收键盘
-(void)OnTapDone{
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    table .frame =CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];
    [_remark resignFirstResponder];
    [self.view endEditing:YES];
}

-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        [imagess removeFromSuperview];
        imagess=[[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 20, 16)];
        imagess.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
        [_remark addSubview:imagess];
        remarkLabel.text = @"备注";
        
    }else{
        [imagess removeFromSuperview];
        remarkLabel.text = @"";
    }
}



@end
